package pratise;

/**
 * @author: lwg
 * @Date: 2021/6/4 14:39
 * @Description: 二维数组中的查找
 */
public class demo2 {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        //1.首先判断二维数组是否为空
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        //2.以右上角的坐标为起始值与目标值比较
        int rows = matrix.length;
        int columns = matrix[0].length ;

        int n = 0;
        int m = columns - 1;
        while (n < rows && m >= 0) {
            //3.比目标值小则向下挪，比目标值大则向右挪
            if (matrix[n][m] == target) {
                return true;
            }
            if (matrix[n][m] < target) {
                n++;
            } else {
                m--;
            }
        }
        return false;
    }
}
